/**
 * 3. 无重复字符的最长子串
 *  https://leetcode.cn/problems/longest-substring-without-repeating-characters/description/?envType=study-plan-v2&envId=top-100-liked
 */
class Solution3 {
    public int lengthOfLongestSubstring(String s) {
        int n = s.length(), ret = 0, left = 0, right = 0;
        char[] ch = s.toCharArray();
        int[] hash = new int[128];

        while(right < n) {
            // 1、进窗口
            hash[ch[right]]++;
            // 2、判断
            while(hash[ch[right]] > 1) {
                hash[ch[left++]]--;
            }
            // 3、更新结果
            ret = Math.max(ret, right - left + 1);
            right++;
        }

        return ret;
    }
}